import { createRouter, createWebHashHistory, type RouteRecordRaw } from 'vue-router'

/** 默认布局 */
export const Layout = () => import('@/layout/index.vue')

/** 静态路由 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: '/redirect',
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    meta: { hidden: true }
  },
  {
    path: '/:pathMatch(.*)*',
    component: () => import('@/views/error/404.vue'),
    meta: { hidden: true }
  },
  {
    path: '/403',
    component: () => import('@/views/error/403.vue'),
    meta: { hidden: true }
  },
  // {
  //   path: '/',
  //   name: 'Home',
  //   component: Layout,
  //   redirect: '/home',
  //   meta: { hidden: false },
  //   children: [
  //     {
  //       path: '/home',
  //       component: () => import('@/views/home/index.vue'),
  //       name: 'Home',
  //       meta: { title: '首页', icon: 'icon-dashboard', svgIcon: 'menu-home', affix: true, hidden: false }
  //     }
  //   ]
  // },
  {
    path: '/',
    component: Layout,
    redirect: '/thesaurus-management/keyword-list',
    meta: {
      title: '词库管理', icon: 'icon-list', affix: false, hidden: false,
    },
    children: [
      {
        path: '/thesaurus-management/keyword-list',
        component: () => import('@/views/thesaurus-management/keyword-list.vue'),
        meta: {
          title: '关键词列表', icon: 'icon-bookmark', affix: false, hidden: false,
        }
      }
    ],
  },
  {
    path: '/audit-management',
    component: Layout,
    redirect: '/audit-management/article-list',
    meta: {
      title: '审核管理', icon: 'icon-stamp', affix: false, hidden: false,
    },
    children: [
      {
        path: '/audit-management/article-list',
        component: () => import('@/views/audit-management/article-list.vue'),
        meta: {
          title: '文章列表', icon: 'icon-list', affix: false, hidden: false,
        }
      },
      {
        path: '/audit-management/advisory-list',
        component: () => import('@/views/audit-management/advisory-list.vue'),
        meta: {
          title: '资讯列表', icon: 'icon-list', affix: false, hidden: false,
        }
      },
      {
        path: '/audit-management/Feigaoku',
        component: () => import('@/views/audit-management/Feigaoku.vue'),
        meta: {
          title: '废稿库', icon: 'icon-list', affix: false, hidden: false,
        }
      },
      {
        path: '/audit-management/relationKeyWords',
        component: () => import('@/views/audit-management/components/relationKeyWords.vue'),
        meta: {
          title: '关联关键词', icon: 'icon-list', affix: false, hidden: true,
        }
      }
    ],
  },
  {
    path: '/audit-management/approve-page',
    component: () => import('@/views/audit-management/approve-page.vue'),
    meta: {
      title: '审批页面', icon: 'icon-list', affix: false, hidden: true,
    }
  },
  {
    path: '/template-management',
    component: Layout,
    redirect: '/template-management/ai-template',
    meta: {
      title: '模板管理', icon: 'icon-interaction', affix: false, hidden: false,
    },
    children: [
      {
        path: '/template-management/ai-template',
        component: () => import('@/views/template-management/ai-template.vue'),
        meta: {
          title: 'AI模板', icon: 'icon-interaction', affix: false, hidden: false,
        }
      },
      {
        path: '/template-management/crate',
        component: () => import('@/views/template-management/crateOrEdit.vue'),
        meta: {
          title: '创建模板', icon: 'icon-interaction', affix: false, hidden: true,
        }
      },
      {
        path: '/template-management/edit',
        component: () => import('@/views/template-management/crateOrEdit.vue'),
        meta: {
          title: '编辑模板', icon: 'icon-interaction', affix: false, hidden: true,
        }
      }
    ],
  },
  {
    path: '/crawl-management',
    component: Layout,
    redirect: '/crawl-management/crawl-list',
    meta: {
      title: '爬取管理', icon: 'icon-bug', affix: false, hidden: false,
    },
    children: [
      {
        path: '/crawl-management/crawl-list',
        component: () => import('@/views/crawl-management/crawl-list.vue'),
        meta: {
          title: '爬取库', icon: 'icon-list', affix: false, hidden: false,
        }
      },
      {
        path: '/crawl-management/crawl-source',
        component: () => import('@/views/crawl-management/crawl-source.vue'),
        meta: {
          title: '爬取源', icon: 'icon-list', affix: false, hidden: false,
        }
      },
      {
        path: '/crawl-management/crawlDetail',
        component: () => import('@/views/crawl-management/crawlDetail.vue'),
        meta: {
          title: '文章详情', icon: 'icon-list', affix: false, hidden: true,
        }
      },
    ],
  },
  {
    path: '/safety-management',
    component: Layout,
    redirect: '/safety-management/employee-management',
    meta: {
      title: '安全管理', icon: 'icon-safe', affix: false, hidden: false, keepAlive: false
    },
    children: [
      {
        path: '/safety-management/employee-management',
        component: () => import('@/views/safety-management/employee-management.vue'),
        meta: {
          title: '员工管理', icon: 'icon-user', affix: false, hidden: false, keepAlive: false
        }
      },
    ],
  },
  {
    path: '/material-library',
    component: Layout,
    redirect: '/material-library/stock-imagery',
    meta: {
      title: '素材库', icon: 'icon-folder', affix: false, hidden: false, keepAlive: false
    },
    children: [
      {
        path: '/material-library/stock-imagery',
        component: () => import('@/views/material-library/stock-imagery.vue'),
        meta: {
          title: '图片素材库', icon: 'icon-image', affix: false, hidden: false, keepAlive: false
        }
      },
    ],
  },
]

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: constantRoutes,
  scrollBehavior: () => ({ left: 0, top: 0 })
})

/**
 * @description 重置路由
 * @description 注意：所有动态路由路由必须带有 name 属性，否则可能会不能完全重置干净
 */
export function resetRouter() {
  try {
    router.getRoutes().forEach((route) => {
      const { name } = route
      // console.log('name', name, path)
      if (name && name !== 'Home') {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch (error) {
    // 强制刷新浏览器也行，只是交互体验不是很好
    window.location.reload()
  }
}

export default router
